package edu.ccut.saturn.studio.naviagtor.wizard.project;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.WorkbenchException;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;

/**
 * Saturn工程向导
 * 
 * @author alf 2008-6-24
 */
public class SaturnProjectNewWizard extends Wizard implements INewWizard {

	private SaturnProjectNewWizardPage page;

	private IWorkbench fWorkbench;

	public SaturnProjectNewWizard() {
		super();
		setNeedsProgressMonitor(true);
	}

	public void addPages() {
		setWindowTitle(Messages.getString("SaturnProjectNewWizard.createcaseproject")); //$NON-NLS-1$
		page = new SaturnProjectNewWizardPage();
		addPage(page);
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		fWorkbench = workbench;
	}

	public boolean performFinish() {
		if (this.page.validate()) {
			try {
				fWorkbench.showPerspective(
						"cn.edu.ccut.CNFSaturnContent.SaturnProjectPerspective", //$NON-NLS-1$
						fWorkbench.getActiveWorkbenchWindow());
			} catch (WorkbenchException e) {
				e.printStackTrace();
			}

			final String projectName = page.getProjectName();
			final String companyName = page.getCompanyName();
			final String version = page.getVersionText();

			final IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
					.getRoot();

			try {
				this.getContainer().run(true, false,
						new IRunnableWithProgress() {
							public void run(IProgressMonitor monitor)
									throws InvocationTargetException,
									InterruptedException {

								monitor
										.beginTask(
												Messages
														.getString("SaturnProjectNewWizard.createcaseproject") + "......", 100); //$NON-NLS-1$ //$NON-NLS-2$

								DefaultGenerateFiles
										.generateDirectory(projectName);

								monitor
										.setTaskName(Messages
												.getString("SaturnProjectNewWizard.createcaseproject") + ":lib"); //$NON-NLS-1$ //$NON-NLS-2$
								monitor.internalWorked(10);

								Map<String, String> values = new HashMap<String, String>();
								values.put("project", projectName); //$NON-NLS-1$
								values.put("companyname", companyName); //$NON-NLS-1$
								values.put("version", version); //$NON-NLS-1$
								values.put("package", projectName.replace(Messages.getString("SaturnProjectNewWizard.15"), //$NON-NLS-1$ //$NON-NLS-2$
										File.separator));

								String fullProjectPath = root.getLocation()
										.toOSString()
										+ File.separator + projectName;

								DefaultGenerateFiles.generateFile(
										fullProjectPath, values);

								SaturnProjectUtil.refreshProject(projectName); //$NON-NLS-1$ //$NON-NLS-2$
								final IProject project = root
										.getProject(projectName);

								try {
									project.create(null);
									project.open(null);
									addProjectNature(project);
									project.close(null);
									project.open(null);
								} catch (CoreException e) {
									e.printStackTrace();
								}

								monitor.done();
							}
						});

			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return true;
		} 
		
		return false;
	}

	private void addProjectNature(IProject project) throws CoreException {
		if (!project.hasNature(SaturnProjectNature.PROJECT_NATURE)) {
			try {
				IProjectDescription description = project.getDescription();
				String[] natures = description.getNatureIds();
				String[] newNatures = new String[natures.length + 1];
				System.arraycopy(natures, 0, newNatures, 0, natures.length);
				newNatures[natures.length] = SaturnProjectNature.PROJECT_NATURE;
				description.setNatureIds(newNatures);
				project.setDescription(description, null);
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
	}
}
